package com.jiunuan.live.api.controller;

import com.jiunuan.live.api.service.ILivingRoomService;
import com.jiunuan.live.api.vo.LivingRoomInitVO;
import com.jiunuan.live.api.vo.req.LivingRoomReqVO;
import com.jiunuan.live.api.vo.resp.LivingRoomPageRespVO;
import com.jiunuan.live.common.interfaces.vo.WebResponseVO;
import com.jiunuan.live.web.starter.context.JiuNuanRequestContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: JiuNuan
 * @CreateTime: 2024-08-19 14:22
 * @Description: 直播间相关接口
 */
@RestController
@RequestMapping("/living")
@Tag(name = "living-直播间相关接口")
public class LivingRoomController {
    @Resource
    private ILivingRoomService livingRoomService;
    @Operation(summary = "list-加载直播间列表")
    @PostMapping("/list")
    public WebResponseVO list(LivingRoomReqVO livingRoomReqVO){
        if(livingRoomReqVO == null || livingRoomReqVO.getType() == null){
            return WebResponseVO.errorParam("需要给定直播间的类型");
        }
        if(livingRoomReqVO.getPage() <=0 || livingRoomReqVO.getPageSize() > 100){
            return WebResponseVO.errorParam("分页参数不合法");
        }
        LivingRoomPageRespVO livingRoomPageRespVO = livingRoomService.list(livingRoomReqVO);
        return WebResponseVO.success(livingRoomPageRespVO);
    }

    @Operation(summary = "startingLiving-开始直播")
    @PostMapping("/startingLiving")
    public WebResponseVO startingLiving(Integer type) {
        // 调用rpc，往开播表写入一条记录
        if(type == null){
            return WebResponseVO.errorParam("需要给定直播间的类型");
        }
        Integer roomId = livingRoomService.startingLiving(type);
        LivingRoomInitVO initVO = new LivingRoomInitVO();
        initVO.setRoomId(roomId);
        return WebResponseVO.success(initVO);
    }
    @Operation(summary = "closeLiving-结束直播")
    @PostMapping("/closeLiving")
    public WebResponseVO closeLiving(Integer roomId) {
        // 调用rpc，往关播表写入一条记录
        if(roomId == null){
            return WebResponseVO.errorParam("需要给定直播间Id");
        }
        boolean closeStatus = livingRoomService.closeLiving(roomId);
        if(closeStatus){
            return WebResponseVO.success();
        }
        return WebResponseVO.bizError("关闭异常");
    }

    /**
     * 获取主播相关配置信息（只有主播才会有权限）
     * @param roomId
     * @return
     */
    @Operation(summary = "anchorConfig-主播初始化")
    @PostMapping("/anchorConfig")
    public WebResponseVO anchorConfig(Integer roomId){
        Long userId = JiuNuanRequestContext.getUserId();
        return WebResponseVO.success(livingRoomService.anchorConfig(userId,roomId));
    }
}
